package io.hepu.robotize.base;

import io.hepu.robotize.page.PageRequest;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * Common methods for database operation.
 *
 * @param <T>  Entity type.
 * @param <ID> Primary key.
 */
public interface IBaseRepository<T, ID extends Serializable> {
    @Select("SELECT id FROM ${table} ORDER BY id DESC LIMIT 1")
    ID selectLastId(@Param("table") String table);

    Integer insert(T entity);

    Integer insertSelective(T entity);

    Integer deleteById(ID id);

    Integer deleteSelective(T entity);

    Integer updateById(T entity);

    Integer updateSelective(T entity);

    T selectById(ID id);

    T selectOne(T entity);

    void deleteSome(ID[] ids);

    void deleteSoft(ID[] ids);

    Integer saveOrUpdate(T entity);

    @Select("SELECT * FROM ${table}")
    List<T> list(@Param("table") String table, @Param("conditions") Map<String, Object> conditions);

    @Select("SELECT * FROM ${table}")
    List<T> paginate(@Param("table") String table, @Param("request") PageRequest request);

    @Select("SELECT COUNT(*) FROM ${table}")
    long total(@Param("table") String table, @Param("conditions") Map<String, Object> conditions);

    <E> List<E> search(@Param("request") PageRequest request);
}
